package cn.iocoder.yudao.module.scrm.controller.admin.marketing;

import cn.hutool.core.lang.tree.Tree;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.category.MarketingCategoryPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.category.MarketingCategoryRespVO;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.category.MarketingCategorySaveReqVO;
import groovy.lang.Lazy;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingCategoryDO;
import cn.iocoder.yudao.module.scrm.service.marketing.category.MarketingCategoryService;

@Tag(name = "管理后台 - 营销分类")
@RestController
@RequestMapping("/scrm/marketing-category")
@Validated
public class MarketingCategoryController {

    @Resource
    @Lazy
    private MarketingCategoryService marketingCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建营销分类")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-category:create')")
    public CommonResult<String> createMarketingCategory(@Valid @RequestBody MarketingCategorySaveReqVO createReqVO) {
        return success(marketingCategoryService.createMarketingCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新营销分类")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-category:update')")
    public CommonResult<Boolean> updateMarketingCategory(@Valid @RequestBody MarketingCategorySaveReqVO updateReqVO) {
        marketingCategoryService.updateMarketingCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除营销分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:marketing-category:delete')")
    public CommonResult<Boolean> deleteMarketingCategory(@RequestParam("id") Integer id) {
        marketingCategoryService.deleteMarketingCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得营销分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-category:query')")
    public CommonResult<MarketingCategoryRespVO> getMarketingCategory(@RequestParam("id") Integer id) {
        MarketingCategoryDO marketingCategory = marketingCategoryService.getMarketingCategory(id);
        return success(BeanUtils.toBean(marketingCategory, MarketingCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得营销分类分页")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-category:query')")
    public CommonResult<PageResult<MarketingCategoryRespVO>> getMarketingCategoryPage(@Valid MarketingCategoryPageReqVO pageReqVO) {
        PageResult<MarketingCategoryDO> pageResult = marketingCategoryService.getMarketingCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MarketingCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出营销分类 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-category:export')")
    @OperateLog(type = EXPORT)
    public void exportMarketingCategoryExcel(@Valid MarketingCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MarketingCategoryDO> list = marketingCategoryService.getMarketingCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "营销分类.xls", "数据", MarketingCategoryRespVO.class,
                        BeanUtils.toBean(list, MarketingCategoryRespVO.class));
    }


    @GetMapping("/treePage")
    @Operation(summary = "获取树形分类")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-category:query')")
    public CommonResult<List<Tree<String>>> getTreeMarketingCategoryPage(@Valid MarketingCategoryPageReqVO pageReqVO) {
        List<Tree<String>> pageResult = marketingCategoryService.getMarketingCategoryTreePage(pageReqVO);
        return success(pageResult);
    }

}